package com.senter.demo.uhf.common;

import java.util.ArrayList;
import java.util.HashMap;

import com.senter.demo.uhf.R;

import android.app.Activity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

/**
 * 说明
 * 
 * @author 
 * @version 创建时间：2013-7-11 下午2:22:38
 */
public class RecordsBoard
{
	private final Activity owner;

	private final ListView lv;
	private final ArrayList<HashMap<String, String>> nd = new ArrayList<HashMap<String, String>>(); // 没有进行重叠显示的数据
	private final ArrayList<HashMap<String, String>> od = new ArrayList<HashMap<String, String>>(); // 选定进行重叠显示的数据

	private final SimpleAdapter na; // 没有进行重叠显示的适配器
	private final SimpleAdapter oa; // 选定进行重叠显示的适配器

	private final TextView tvCount;
	public RecordsBoard(Activity context, View ll)
	{
		owner = context;

		na = new SimpleAdapter(owner, nd, R.layout.common_lv2overlap_tv1, new String[] { "0" }, new int[] { R.id.idE2CommenLv2OverlapTv1_tv0 });
		oa = new SimpleAdapter(owner, od, R.layout.common_lv2overlap_tv2, new String[] { "0", "1" }, new int[] { R.id.idE2CommenLv2OverlapTv1_tv0, R.id.idE2CommenLv2OverlapTv1_tv1 });

		lv = (ListView) ll.findViewById(R.id.idE2CommenLv2Overlap_lvShow);
		lv.setAdapter(na);

		final CheckBox cbOl = (CheckBox) ll.findViewById(R.id.idE2CommenLv2Overlap_cbOverlap);
		cbOl.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{// 依据不同的显示方式选用适配器
				if (cbOl.isChecked())
				{
					lv.setAdapter(oa);
				} else
				{
					lv.setAdapter(na);
				}
			}
		});
		tvCount=(TextView) ll.findViewById(R.id.idE2CommenLv2Overlap_tvTagsCount);
	}

	public void addMassage(	final String string)
	{
		owner.runOnUiThread(new Runnable()
		{

			@Override
			public void run()
			{
				HashMap<String, String> map = new HashMap<String, String>();
				map.put("0", string);
				map.put("1", "" + 1);

				nd.add(map);

				int index = 0;
				for (; index < od.size(); index++)
				{// 更新重叠显示数据
					HashMap<String, String> omap = od.get(index);
					if (omap.get("0").equals(string))
					{// 找到
						int num = 1;

						String numString = omap.get("1");
						if (numString != null)
						{
							try
							{
								num = Integer.valueOf(numString);
							}
							catch (Exception e)
							{
								e.printStackTrace();
							}
						}
						num++;
						omap.put("1", "" + num);
						break;
					}
				}
				if (index == od.size())
				{// 没找着
					od.add(map);
				}
				tvCount.setText(""+od.size());

				na.notifyDataSetChanged();
				oa.notifyDataSetChanged();
			}
		});
	}

	public void clearMsg()
	{// 清除数据
		owner.runOnUiThread(new Runnable()
		{
			@Override
			public void run()
			{
				nd.clear();
				od.clear();
				tvCount.setText(""+od.size());
				na.notifyDataSetChanged();
				oa.notifyDataSetChanged();
			}
		});
	}

	public ArrayList<HashMap<String, String>> getData()
	{
		ArrayList<HashMap<String, String>> ret = new ArrayList<HashMap<String, String>>();
		ret.addAll(nd);
		return ret;
	}

	public boolean isEmpty()
	{// 只需要检查一个数据就可以知道是否为空
		return nd.isEmpty();
	}
}
